package com.zyt.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zyt.mapper.StudentMapper;
import com.zyt.pojo.PageResult;
import com.zyt.pojo.Student;
import com.zyt.pojo.StudentQueryParam;
import com.zyt.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    /*
    *学员违纪处理
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void violation(Integer id, Integer score) {
        studentMapper.violation(id,score);
    }

    /*
     * 根据id查询员工信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Student getInfo(Integer id) {
      return studentMapper.getById(id);

    }

    /*
     * 修改员工信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Student student) {
        //补全基础属性
        student.setUpdateTime(LocalDateTime.now());

        //再插入新数据
        studentMapper.update(student);
    }

    /*
     * 新增员工
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Student student) {
        //补全基础属性
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        //再插入新数据
        studentMapper.insert(student);
    }

    /*
    * 批量删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }

    @Autowired
    private StudentMapper studentMapper;

    /*
     * 分页查询
     */
    @Override
    public PageResult<Student> page(StudentQueryParam studentQueryParam) {
        //设置分页参数
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        //执行查询
        List<Student> rows = studentMapper.rows(studentQueryParam);
        //解析查询结果并封装
        Page<Student> p = (Page<Student>) rows;
        return new PageResult<Student>(p.getTotal(), p.getResult());

    }


}
